<template>
  <el-container>
    <el-header class="container_header">
      <Header />
    </el-header>
    <el-main class="container_main">
      <nuxt-page />
    </el-main>
  </el-container>
</template>

<script setup lang="ts">
  import {useRouter,useRoute} from 'vue-router'
  import { getItem, setItem } from '~/utils'
  import { onMounted,getCurrentInstance } from 'vue'
  import { getProject } from '~/api/project'
  import {screenStore} from '~/stores/modules/screen'
  import {getHistoryProject,setHistoryProject} from '~/api/public'
  const router = useRouter()
  const route = useRoute()
  const screensStore = screenStore()
  useHead({
    title: '神秘客'
  })
  const instance = getCurrentInstance();
  onMounted(()=>{
    console.log('222222')
    pageLoadHandle()
  })

  function pageLoadHandle(){
    if(!sessionStorage.getItem('showLogin')){
      getProject(getItem('pinpaiId')).then(res => {
        if(res.code == 200){
          if(res.data && res.data.length){
            screensStore.projectSet = res.data
            if(res.data.length == 1){
              screensStore.selProjectId = res.data[0].id
              screensStore.selProjectObj = res.data[0]
            }
            else{
              getHistoryProject({brandId:getItem('pinpaiId')}).then(result => {
                if(result.code == 200){
                  if(result.data){
                    screensStore.selProjectId = result.data.projectId
                    const ind = res.data.findIndex(item => item.id == result.data.projectId)
                    screensStore.selProjectObj = res.data[ind]
                  }
                  else{
                    screensStore.selProjectId = res.data[0].id
                    screensStore.selProjectObj = res.data[0]
                  }
                }
                else{
                  screensStore.selProjectId = res.data[0].id
                  screensStore.selProjectObj = res.data[0]
                }
              })
            }
            const index = screensStore.menuNav.findIndex(item => {
              let path = route.path
              if(route.path.includes('onestatementview')){
                const pathArr = route.path.split('/')
                const ind = screensStore.menuNav.findIndex(row => row.path.includes(pathArr[1]))
                path = screensStore.menuNav[ind].path
              }
              if(route.path.includes('totalstatementview')){
                const pathArr = route.path.split('/')
                const ind = screensStore.menuNav.findIndex(row => row.path.includes(pathArr[1]))
                path = screensStore.menuNav[ind].path
              }
              if(item.path == path){
                return true
              }
            })
            console.log(index,'index')
            if(index != -1){
              if(screensStore.menuNav[index].name != '报表查看'){
                screensStore.activeHomeNav = screensStore.menuNav[index].name
              }
              else{
                let indChild = screensStore.menuNav[index].children.findIndex(item => route.path.includes(item.path))
                screensStore.activeHomeNav = screensStore.menuNav[index].children[indChild].meta.title
              }
              instance.proxy.$forceUpdate();
            }
          }
          else{
            screensStore.menuNav = [
              { name: '项目概述', children: null,path:'/projectdesp' },
              { name: '期次选择', children: null,path:'/issueselect' },
            ]
            ElMessage({
              type:'error',
              message:'你当前登录的品牌下暂无项目，请联系管理员！',
              showClose: true,
              duration:0,
            })
          }
        }
        else{
          setTimeout(()=>{
            router.push({
              path: '/login?id='+getItem('pinpaiId'),
            })
          },2000)
        }
      })
      screensStore.bodyWidth = document.body.scrollWidth
      screensStore.bodyHeight = document.body.scrollHeight
      sessionStorage.setItem('showLogin','已登录')
    }
  }
</script>

<style scoped lang="less">
  .container_header {
    padding: 0;
    height: 125px;
  }

  .container_main {
    height: calc(100vh - 125px);
    overflow-y: auto;
  }
</style>
